﻿
// Condition: You are given an array of strings. Write a method that sorts the array by the length of its elements (the number of characters composing them).

using System;
using System.Collections.Generic;


class StringArraySortm
{
    // write a class that make a special sort
    class StringComparer : IComparer<string>
    {
        public int Compare(string string1, string string2)
        {
            int strCompare1 = string1.Length;
            int strCompare2 = string2.Length;

            // exception that sort alphabetically
            if (strCompare1 == strCompare2)
            {
                return string1.CompareTo(string2);
            }
            // return first string with less characters 
            return strCompare1.CompareTo(strCompare2);
        }
    }

    static void Main(string[] args)
    {
        string[] stringsValue = { "monitor", "software", "table",
                            "mouse", "pen", "antivirus", "pad"};
        Console.WriteLine("Initial array:");
        foreach (string stringValue in stringsValue)
        {
            Console.Write("{0} - {1} elements", stringValue, stringValue.Length);
            Console.WriteLine();
        }

        Console.WriteLine();
        Console.WriteLine("Array after sorting:");

        Array.Sort(stringsValue, new StringComparer());
        foreach (string stringValue in stringsValue)
        {
            Console.Write("{0} - {1} elements", stringValue, stringValue.Length);
            Console.WriteLine();
        }
    }
}

